package com.turing.common.util;


public class TextUtils {

    /* 给前端用的方法，寻找原始字符串向前或向后扩展后的字符串
     * searchWords:需要扩展的词
     * text：来源文本
     * expandForward：向前扩展offset
     * expandBackward：向后扩展offset
     * */
    public static String expandTextLimit(String searchWords, String text, int expandForward, int expandBackward) {

        if (expandForward <= 0 || expandBackward <= 0 || searchWords.length() >= text.length()) {
            return searchWords;
        }

        String strForward;
        String strBackWord;

        int start = text.indexOf(searchWords);
        if (start >= expandForward) {
            strForward = text.substring(start - expandForward, start);
        } else {
            strForward = text.substring(0, start);
        }

        int end = start + searchWords.length() - 1;
        if (end < text.length() - expandBackward) {
            strBackWord = text.substring(end + 1, end + expandBackward + 1);
        } else {
            strBackWord = text.substring(end + 1);
        }

        return strForward + searchWords + strBackWord;

    }


    public static void main(String[] args) {
        System.out.println(expandTextLimit("鸭", "门前大桥下游过一群鸭，快来快来数一数二四六七八", 5, 5));
    }

}
